package ${package.Controller};

import ${package.Entity}.${entity};

import com.ruoyi.common.core.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@RestController
@RequestMapping("/system/${table.entityPath}")
public class ${table.controllerName} extends BaseController{

    @Resource
    private ICangKuService service;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(${entity} user)
    {
        startPage();
        List<${entity}> list = service.selectUserList(user);
        return getDataTable(list);
    }


    /**
     * 获取用户列表,不分页
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll()
    {
        ${entity} user = new ${entity}();
        user.setParams(new HashMap<>());
        user.getParams().put(TableSupport.PAGE_NUM, 1);
        user.getParams().put(TableSupport.PAGE_SIZE, 100000);

        startPage();
        List<${entity}> list = service.selectList(user);
        return getDataTable(list);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(userId))
        {
            CangKu user = service.selectUserById(userId);
            ajax.put(AjaxResult.DATA_TAG, user);
        }
        return ajax;
    }

    /**
     * 新增用户
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CangKu user)
    {
        if (service.haveUserName(user))
        {
            return error("新增用户'" + user.getName() + "'失败，登录账号已存在");
        }
        user.setCreateBy(getUsername());
        user.setCreateTime(new Date());
        return toAjax(service.insertUser(user));
    }


    /**
     * 修改用户
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CangKu user)
    {
        if (!service.haveUserName(user))
        {
            return error("修改用户'" + user.getName() + "'失败，登录账号已存在");
        }
        user.setUpdateBy(getUsername());
        user.setUpdateTime(new Date());
        return toAjax(service.updateById(user));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        if (ArrayUtils.contains(userIds, getUserId()))
        {
            return error("当前用户不能删除");
        }
        return toAjax(service.deleteUserByIds(userIds));
    }
}
